//
//  NewFeatureViewController.swift
//  MyWeiboApp
//
//  Created by 胡海波 on 15/6/29.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit
import FFAutoLayout

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    let layout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)

        // 设置布局
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal

        // 设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }



    // MARK: UICollectionViewDataSource

    let imageCount = 4

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

       return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
    
        return cell
    }

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        // 获取当前显示的cell
        let path = collectionView.indexPathsForVisibleItems().last!

        if path.item == imageCount - 1 {

            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).showStartButton()
        }
    }

}

class NewFeatureCell:UICollectionViewCell {



    // 图像索引
    var imageIndex: Int = 0 {

        didSet {

            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }

    // 动画显示开始按钮
    func showStartButton() {
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.hidden = false
        startButton.userInteractionEnabled = false

        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in

            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
                self.startButton.userInteractionEnabled = true
        }

    }

    func startButtonClicked() {

        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: true)
    }

    // 懒加载 开始按钮
    lazy var startButton: UIButton = {

        // 自定义按钮
        let btn = UIButton()

        btn.frame = CGRectMake(0, 0, 105, 36)

        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "startButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()

    lazy var iconView: UIImageView = {

        return UIImageView()
    }()

    override init(frame: CGRect) {

        super.init(frame: frame)

        addSubview(iconView)
        addSubview(startButton)

        // 自动布局
        startButton.ff_AlignInner(ff_AlignType.BottomCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -160))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {

        super.layoutSubviews()

        iconView.frame = self.bounds

    }
}
